# -*- coding: utf-8 -*-
import pandas as pd
from MySQLdb import connect, cursors
import configparser
import os
import seaborn as sns
import matplotlib.pyplot as plt
import socket

def construct_db_connections():
    """进行数据库的连接设置，使用的是sql库,输出是库表连接，用于连接数据库
    Returns
    -------
    jy_conn: TYPE
        DESCRIPTION.
    zj_conn: TYPE
        DESCRIPTION.

    """
    hostname = socket.gethostname()
    hostname_prefix = hostname.split('-')[0]
    is_server = hostname_prefix in ('dev00', 'online00', 'online01')

    if not is_server:
        """
        In local environment, you should put a configuration file named
        '.db_config.ini' in the current directory.
        The configuration file content format must be:
            [DEFAULT]
            user=<YOUR_DB_USERNAME>
            password=<YOUR_PASSWORD>
            schema=<YOUR_OWN_SCHEMA>

        NOTE: Please do NOT commit the configuration file to svn
        """
        pwd = os.getcwd()
        db_config_file_path = os.path.join(pwd, '.db_config.ini')
        assert os.path.exists(db_config_file_path), "db config file %s is not existed" % db_config_file_path
        parser = configparser.ConfigParser()
        parser.read(db_config_file_path)
        user = parser['DEFAULT']['user']
        password = parser['DEFAULT']['password']
        zj_schema = parser['DEFAULT']['schema']
        db_host = 'dev.zhijuninvest.com'
    else:
        user = 'cronjob'
        password = 'ZyYADQjvh68UjySZ'
        db_host = '172.17.0.1'
        zj_schema = 'zj_data'

    jy_conn = connect(host=db_host, port=3306, user=user, passwd=password,
                      db="JYDB", charset="utf8mb4", cursorclass=cursors.DictCursor)
    zj_conn = connect(host=db_host, port=3306, user=user, passwd=password,
                      db=zj_schema, charset="utf8mb4", cursorclass=cursors.DictCursor)

    return jy_conn, zj_conn







